package com.chidopi.cdf.util;

import org.apache.log4j.Logger;
import org.apache.log4j.RollingFileAppender;
import org.apache.log4j.spi.LocationInfo;
import org.apache.log4j.spi.LoggingEvent;

public class DynaRollingAppender extends RollingFileAppender {
	private Logger logger1 = Logger.getLogger(this.getClass().getName());
	private static String oldFileName = null;
    private static String oldPath = null;
    public void generateLogFile(LocationInfo info) {
        if (oldPath == null) {
            oldPath = this.fileName.substring(0, this.fileName.lastIndexOf("test"));
        }
        String fileName1 = oldPath;
        fileName1 += info.getClassName() + ".log";
        this.logger1.info(fileName1 + "; " + this.fileName);
        oldFileName = fileName1;
        this.setFile(fileName1);
        this.activateOptions();
    }


    @Override
    protected void subAppend(LoggingEvent event) {
        String className =  event.getLocationInformation().getClassName();
        if (oldFileName != className) {
            this.generateLogFile(event.getLocationInformation());
        }
        super.subAppend(event);
    }

}
